package ru.arriah.servicedesk.ajax;

import java.util.Collection;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.naming.InitialContext;

import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;

public class AjaxHandler {
  protected OrganizationManagerLocalHome organizationManagerHome;
  protected ClientManagerLocalHome clientManagerHome;
  protected Properties config;

  public AjaxHandler() throws Exception {
    organizationManagerHome = 
      (OrganizationManagerLocalHome) (new InitialContext()).lookup(
          "java:comp/env/ejb/OrganizationManager"
      );
    
    clientManagerHome =
      (ClientManagerLocalHome) (new InitialContext()).lookup(
         "java:comp/env/ejb/ClientManager"
      );    
  }

  
  /**
   * Select departments by organization id
   * @param organizationId
   * @return departments collection
   * @throws Exception
   */
  public Collection<DepartmentBean> getDepartments(int organizationId) throws Exception {
    try {          
      OrganizationManagerLocalObject organizationManager = 
        organizationManagerHome.create();
      Collection<DepartmentBean> departmentsList = 
        organizationManager.selectDepartments(organizationId);      
      organizationManager.remove();
      return departmentsList;
    } catch (Exception e) {
      throw new Exception("ErrorMessage.internal", e);
    }
  }
  
  
  /**
   * Select clients by department id
   * @param departmentId
   * @return
   * @throws Exception
   */
  public Collection<ClientBean> getClientsByDepartment(int departmentId) throws Exception {
    try {           
      ClientManagerLocalObject clientManager = 
        clientManagerHome.create();
      Collection<ClientBean> clientsList = 
        clientManager.selectClientsByDepartment(departmentId);
      clientManager.remove();
      return clientsList;
    } catch (Exception e) {
      throw new Exception("ErrorMessage.internal", e);
    }
  }
  
  
  /**
   * Select clients by organization id
   * @param organizationId
   * @return
   * @throws Exception
   */
  public Collection<ClientBean> getClientsByOrganization(int organizationId) throws Exception {
    try {           
      ClientManagerLocalObject clientManager = 
        clientManagerHome.create();
      Collection<ClientBean> clientsList = 
        clientManager.selectClientsByOrganization(organizationId);
      clientManager.remove();
      return clientsList;
    } catch (Exception e) {
      throw new Exception("ErrorMessage.internal", e);
    }
  }
  
  
  /**
   * @deprecated
   * @param value
   * @return
   * @throws Exception
   */
  public boolean isKPPValid(String value) throws Exception {
    Pattern pattern = Pattern.compile("^[0-9]{9}$|(^$)");
    return (pattern.matcher(value).matches());
  }
  
  
  /**
   * @deprecated
   * @param value
   * @return
   * @throws Exception
   */
  public boolean isINNValid(String value) throws Exception {
    Pattern pattern = Pattern.compile("(^[0-9]{12})$|(^$)");
    return (pattern.matcher(value).matches());
  }
  
  
  /**
   * @deprecated
   * @param value
   * @return
   * @throws Exception
   */
  public boolean isBIKValid(String value) throws Exception {
    Pattern pattern = Pattern.compile("^[0-9]{9}$|(^$)");
    return (pattern.matcher(value).matches());
  }
  
  
  /**
   * @deprecated
   * @param value
   * @return
   * @throws Exception
   */
  public boolean isAccountValid(String value) throws Exception {
    Pattern pattern = Pattern.compile("^[0-9]{20}$|(^$)");
    return (pattern.matcher(value).matches());
  }
  
  
  /**
   * @deprecated
   * @param value
   * @return
   * @throws Exception
   */
  public boolean isEmailValid(String value) throws Exception {
    Pattern pattern = Pattern.compile(
      "(^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$)|(^$)", 
      Pattern.CASE_INSENSITIVE
    );    
    return (pattern.matcher(value).matches());
  }
}